namespace ResumableDownload.View.Converters
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    using WP7Contrib.Services.DownloadManager;

    public sealed class FileRequestToButtonContentConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value == DependencyProperty.UnsetValue)
            {
                return DependencyProperty.UnsetValue;
            }

            var request = (FileRequest)value;
            if (request.State == FileRequestState.Failed)
            {
                return "Retry";
            }
            
            var isImage = request.FilePath.ToLower().EndsWith("jpg");
            if (isImage)
            {
                return "View";
            }

            var isMp3 = request.FilePath.ToLower().EndsWith("mp3");
            if (isMp3)
            {
                return "Play";
            }

            return string.Empty;
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }
}